<template>
	<el-main>
		<el-row :gutter="15">
			<el-col :xl="6" :lg="6" :md="8" :sm="12" :xs="24" v-for="item in list" :key="item.taskId">
				<el-card class="task task-item" shadow="hover">
					<div>
						<div style="text-align: center;height:150px;width:120px;display: inline-block">
							<el-image class="image" :src="item.avatar" style="height: 150px;width: 100%"></el-image>
						</div>
						<div style="display: inline-block;margin-left: 20px;position: absolute;">
							<div style="line-height: 30px;font-size: 14px">姓名:{{item.userName}}
								<div style="margin-left: 20px;float: right;">
									<el-tag v-if="item.status=='0'" size="small">空闲</el-tag>
									<el-tag v-if="item.status=='1'" size="small">途中</el-tag>
									<el-tag v-if="item.status=='2'" size="small" type="info">休假</el-tag>
								</div>
							</div>
							<div style="line-height: 30px;font-size: 14px">性名:{{item.sex=='1'?'男':'女'}}</div>
							<div style="line-height: 30px;font-size: 14px">驾驶证:{{item.licenceTypeName}}</div>
							<div style="line-height: 30px;font-size: 14px">发证日期:{{item.issueYear}}</div>
							<div style="line-height: 30px;font-size: 14px">出生日期:{{item.birthday}}</div>
						</div>
					</div>
					<div class="bottom">
						<div class="state" style="font-size: 14px">
							Phone:{{item.phone}}
						</div>
						<div class="handler">
							<el-dropdown trigger="click">
								<el-button type="primary" icon="el-icon-more" circle plain></el-button>
								<template #dropdown>
									<el-dropdown-menu>
										<el-dropdown-item @click="edit(item)">编辑</el-dropdown-item>
										<el-dropdown-item @click="del(item)" divided>删除</el-dropdown-item>
									</el-dropdown-menu>
								</template>
							</el-dropdown>
						</div>
					</div>
				</el-card>
			</el-col>
			<el-col :xl="6" :lg="6" :md="8" :sm="12" :xs="24">
				<el-card class="task task-add" shadow="never" @click="add">
					<el-icon>
						<el-icon-plus/>
					</el-icon>
					<p>创建司机</p>
				</el-card>
			</el-col>
		</el-row>
	</el-main>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
</template>

<script>
import saveDialog from './save'

export default {
	name: 'task',
	components: {
		saveDialog
	},
	provide() {
		return {
			list: this.list
		}
	},
	data() {
		return {
			dialog: {
				save: false
			},
			list: []
		}
	},
	mounted() {
		this.getVehicleDriverList();
	},
	methods: {
		async getVehicleDriverList() {
			var res = await this.$API.office.vehicle.getVehicleDriverList.get();
			this.list = res.data;
		},
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		edit(driver) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(driver)
			})
		},
		async deleteVehicleDriver(driver) {
			const loading = this.$loading();
			var res = await this.$API.office.vehicle.deleteVehicleDriver.post({driverId: driver.driverId});
			if (res.code == 200) {
				this.list.splice(this.list.findIndex(item => item.driverId === driver.driverId), 1)
				this.$message.success(res.message)
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
			loading.close();
		},
		async del(driver) {
			await this.$confirm(`确认删除 ${driver.userName} 吗？`, '提示', {
				type: 'warning',
				confirmButtonText: '删除',
				confirmButtonClass: 'el-button--danger'
			}).then(() => {
				this.deleteVehicleDriver(driver);
			}).catch(() => {
				//取消
			})
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.list.push(data)
			} else if (mode == 'edit') {
				this.list.filter(item => item.driverId === data.driverId).forEach(item => {
					Object.assign(item, data)
				})
			}
		}
	}
}
</script>

<style scoped>
.task {
	height: 230px;
}

.task-item h2 {
	font-size: 15px;
	color: #3c4a54;
	padding-bottom: 15px;
}

.task-item li {
	list-style-type: none;
	margin-bottom: 10px;
}

.task-item li h4 {
	font-size: 12px;
	font-weight: normal;
	color: #999;
}

.task-item li p {
	margin-top: 5px;
}

.task-item .bottom {
	border-top: 1px solid #EBEEF5;
	text-align: right;
	padding-top: 10px;
	display: flex;
	justify-content: space-between;
	align-items: center;
}

.task-add {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	text-align: center;
	cursor: pointer;
	color: #999;
}

.task-add:hover {
	color: #409EFF;
}

.task-add i {
	font-size: 30px;
}

.task-add p {
	font-size: 12px;
	margin-top: 20px;
}

.dark .task-item .bottom {
	border-color: var(--el-border-color-light);
}

.info-title {
	width: 100%;
	overflow: hidden;
	white-space: nowrap;
	text-overflow: ellipsis;
	cursor: pointer;
}
</style>
